package org.stone.project.tx.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.stone.project.tx.prop.TxProp;
import org.stone.project.tx.sysEnum.ReceiveType;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Configuration
@Data
public class RabbitConfig {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper();
    }

    @PostConstruct
    public void init(){
        queueNameUser = concatQueueName(ReceiveType.USER.name());
        queueNameRole = concatQueueName(ReceiveType.ROLE.name());
        queueNameGroup = concatQueueName(ReceiveType.GROUP.name());
        queueNameAll = concatQueueName(ReceiveType.ALL.name());

        txExchangeUser = concatExchangename(ReceiveType.USER.name());
        txExchangeRole= concatExchangename(ReceiveType.ROLE.name());
        txExchangeGroup = concatExchangename(ReceiveType.GROUP.name());
        txExchangeAll = concatExchangename(ReceiveType.ALL.name());
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter(objectMapper()));
    }

    @Autowired
    private TxProp txProp;

    private String txExchangePrefix ="fanout.txExchangePrefix";

    private String txExchangeUser;

    private String txExchangeRole;

    private String txExchangeGroup;

    private String txExchangeAll;



    private String queueNameUser;

    private String queueNameRole;

    private String queueNameGroup;

    private String queueNameAll;

    /**
     * 队列名称
     * @param type
     * @return
     */
    public String concatQueueName(String type){
        return type+"_"+txProp.getLocalIp()+txProp.getPort();
    }

    /**
     * 交换机名称
     * @param receiveType
     * @return
     */
    public String concatExchangename(String receiveType){
        return txExchangePrefix+"_"+receiveType;
    }

    /**
     * 单发
     * @return
     */
    @Bean
    public FanoutExchange userFanoutExchange() {
        return new FanoutExchange(concatExchangename(ReceiveType.USER.name()));
    }

    @Bean
    public Queue userQueue() {
        return new Queue(concatQueueName(ReceiveType.USER.name()));
    }

    @Bean
    public Binding binding1(Queue userQueue, FanoutExchange userFanoutExchange) {
        return BindingBuilder.bind(userQueue).to(userFanoutExchange);
    }


    /**
     * 群发
     * @return
     */
    @Bean
    public FanoutExchange roleFanoutExchange() {
        return new FanoutExchange(concatExchangename(ReceiveType.ROLE.name()));
    }

    @Bean
    public Queue roleQueue() {
        return new Queue(concatQueueName(ReceiveType.ROLE.name()));
    }

    @Bean
    public Binding binding2(Queue roleQueue, FanoutExchange roleFanoutExchange) {
        return BindingBuilder.bind(roleQueue).to(roleFanoutExchange);
    }



    @Bean
    public FanoutExchange groupFanoutExchange() {
        return new FanoutExchange(concatExchangename(ReceiveType.GROUP.name()));
    }

    @Bean
    public Queue groupQueue() {
        return new Queue(concatQueueName(ReceiveType.GROUP.name()));
    }

    @Bean
    public Binding binding3(Queue groupQueue, FanoutExchange groupFanoutExchange) {
        return BindingBuilder.bind(groupQueue).to(groupFanoutExchange);
    }


    @Bean
    public FanoutExchange allFanoutExchange() {
        return new FanoutExchange(concatExchangename(ReceiveType.ALL.name()));
    }

    @Bean
    public Queue allQueue() {
        return new Queue(concatQueueName(ReceiveType.ALL.name()));
    }

    @Bean
    public Binding binding4(Queue allQueue, FanoutExchange allFanoutExchange) {
        return BindingBuilder.bind(allQueue).to(allFanoutExchange);
    }
}
